
package com.qf.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;

import com.qf.dao.OrderDao;
import com.qf.model.OrderItem;
import com.qf.model.Orders;
import com.qf.utils.C3P0Util;

public class OrderDaoImpl implements OrderDao{

	@Override
	public boolean add(Orders o) {
		DataSource datasource = C3P0Util.getDatasource();
		QueryRunner qr = new QueryRunner(datasource);
		Connection conn=null;
		try {
			 conn = datasource.getConnection();
			 conn.setAutoCommit(false);
			 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String sql = "insert into orders values(?,?,?,?,?,?,?,?);";
			 qr.update(conn, sql,new Object[]{o.getOid(),sdf.format(o.getOrdertime()),o.getTotal(),o.getState(),o.getAddress(),o.getName(),o.getTelephone(),o.getUser().getUid()});
			
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		//TODO
		String sql_="insert into orderitem values(?,?,?,?,?);";
		for(OrderItem oi:o.getList())
			try {
				qr.update(conn, sql_, new Object[]{oi.getItemid(),oi.getCount(),oi.getSubtotal(),oi.getProduct(),oi.getOrders()});
			} catch (SQLException e) {
				
				//TODO
				e.printStackTrace();
					
			}
		try {
			conn.commit();
		} catch (SQLException e) {
			
			//TODO
			e.printStackTrace();
				
		}
		return true;
			
	}

}

	